package com.rlax.ccdemo.service.api.user.config;

import com.rlax.ccdemo.service.api.user.fegin.IAuthApi;
import com.rlax.corebin.core.consts.PropertiesConstants;
import com.rlax.corebin.core.result.R;
import com.rlax.corebin.security.entity.AuthUser;
import com.rlax.corebin.security.feign.AuthQuery;
import com.rlax.corebin.security.feign.AuthService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Auth Feign 类型配置类
 * @author Rlax
 *
 */
@Configuration
@ConditionalOnProperty(prefix = PropertiesConstants.PRE_SECURITY, name = "type", havingValue = "FEIGN", matchIfMissing = true)
@EnableFeignClients(basePackageClasses = IAuthApi.class)
public class AuthServiceFeignConfiguration {

    @Bean
    @ConditionalOnMissingBean(AuthService.class)
    public AuthService authService(IAuthApi iAuthApi) {
        return new AuthServiceImpl(iAuthApi);
    }

    /**
     * feign 实现类
     */
    public static class AuthServiceImpl implements IAuthApi {
        private final IAuthApi iAuthApi;

        public AuthServiceImpl(IAuthApi iAuthApi) {
            this.iAuthApi = iAuthApi;
        }

        @Override
        public R<AuthUser> getAuthInfo(Long id, AuthQuery authQuery) {
            return iAuthApi.getAuthInfo(id, authQuery);
        }
    }

}
